<template>
  <!-- 
    <el-row></el-row>一行分为24份
    el-col 表示 :span='12'代表在一行中占12份（50%）
    :offset="3" 代表在一行中左侧margin的份数

    校验相关
 -->
  <el-row class="login-page">
    <el-col :span="12" class="bg"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 注册相关表单 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <!--
            v-model双向绑定 
            prop绑定规则
            :prefix-icon="User"小图标
          -->
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <!-- <el-input
            v-model="formModel.repassword"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
          ></el-input> -->
        </el-form-item>
        <el-form-item>
          <el-button
            @click="register"
            class="button"
            type="primary"
            auto-insert-space
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="// @ts-ignore
            isRegister = false">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>


      <!-- 登录相关表单 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-else
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- :prefix-icon="Lock"图标 -->
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item>
          <el-input></el-input>
        </el-form-item> -->
        <!-- <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item> -->
        <el-form-item>
          <el-button
            @click="login"
            class="button"
            type="primary"
            auto-insert-space
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            @click="showRegisterDrawer"
            class="button"
            type="infp"
            plain
            auto-insert-space
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
  <registerDrawer ref="registerRef"/>
</template>

<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
// import { userRegisterServe, userLoginServe } from '@/api/user'
import { userUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { userGetLogin } from '@/api/article.js'
import registerDrawer from '@/views/Login/registerDrawer.vue'

//获取对话框dom
const registerRef = ref(null);
const showRegisterDrawer = () => {
  // console.log(registerRef.value.openRegisterDrawer);
  registerRef.value.openRegisterDrawer();
}

const formModel = ref({
    username: "hbchen",
    password: "123456",
})

const isRegister = ref(false)
const register =() => {}

const rules = {
    username: [
      {
        required: true,
        message: '请输入用户名',
        trigger: 'blur'
      },
      {
        // min: 5,
        // max: 10,
        //   pattern: /^\S{6,15}$/,
        // message: '用户名必须是5-10位的字符',
        trigger: 'blur'
        //   triggrt: 'chage'为实时的上面的是失焦后校验
      }
    ],
    password: [
      {
        required: true,
        message: '请输入密码',
        trigger: 'blur'
      },
      {
        // min: 6,
        // max: 15,
        // pattern: /^\S{6,15}$/,
        // message: '密码必须是6-15位的非空字符',
        trigger: 'blur'
      }
    ],
    repassword: [
      {
        required: true,
        message: '请输入密码',
        trigger: 'blur'
      },
      {
        min: 6,
        max: 15,
        pattern: /^\S{6,15}$/,
        message: '密码必须是6-15位的非空字符',
        trigger: 'blur'
      },
      {
        // @ts-ignore
        validator: (rule, value, callback) => {
          if (value !== formModel.value.password) {
            callback(new Error('两次输入的密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
}

const form = ref()


// @ts-ignore
const userStores = userUserStore()
const router = useRouter()

const login = async () => {
  await form.value.validate()
  const res = await userGetLogin(formModel.value)
  console.log(res.data.data);
  userStores.setToken(res.data.data.token)
  userStores.setRole(res.data.data.id)
  userStores.setUser(formModel.value.username)
  if(res.data.code == 200){
    ElMessage({
      showClose: true,
      message: '登录成功',
      type: 'success'
    }) 
    router.push('/teamwork')
  }
  else{
    ElMessage({
      showClose: true,
      message: '用户名或密码错误',
      // type: 'error'
    })
  }
}
</script>


<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background:
      url('@/img/logo.jpg') no-repeat 70% center / 339px auto,
      url('@/assets/login_bg.jpg') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
    // background-color: pink;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
